package com.zhongwang.mms.module.half.service;

import com.zhongwang.mms.gen.entity.HImportMarkError;
import com.zhongwang.mms.gen.entity.HImportMarkErrorExample;
import com.zhongwang.mms.gen.entity.HInventory;
import com.zhongwang.mms.gen.entity.HInventoryProjectMark;
import com.zhongwang.mms.module.half.dao.HImportMarkErrorDao;
import com.zhongwang.mms.module.half.dao.HInventoryDao;
import com.zhongwang.mms.module.half.dao.HInventoryProjectMarkDao;
import com.zhongwang.mms.module.half.model.HImportItyToProModel;
import com.zhongwang.mms.module.half.model.HInventoryModel;
import com.zhongwang.mms.module.half.model.HInventoryProjectMarkModel;
import com.zhongwang.mms.module.half.model.HInventoryQueryModel;
import com.zhongwang.mms.module.material.dao.MOrderDetailDao;
import com.zhongwang.mms.module.material.model.MProCodeQueryModel;
import com.zhongwang.mms.module.overview.model.ProjectStatusModel;
import com.zhongwang.mms.util.BigDecimalUtils;
import com.zhongwang.mms.util.ShiroUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.zhongwang.mms.util.PoiUtils.getValue;

/**
 * @description:库存划项目标记
 * @author: th
 * @time: 2020/5/8 9:11
 */
@Log4j2
@Service
public class InvToProMarkService {

    @Autowired
    private HInventoryDao hInventoryDao;
    @Autowired
    private HInventoryProjectMarkDao hInventoryProjectMarkDao;
    @Autowired
    private HImportMarkErrorDao hImportMarkErrorDao;
    @Autowired
    private MOrderDetailDao orderDetailDao;

    /**
     * 新增页面加载数据
     * @param query
     * @return
     */
    public List<HInventory> getInventoryList(HInventoryQueryModel query) {

        query.setItyStockTypeQuery(1);
        query.setItyMatTypeQuery(5);
        query.setItyQuery(1);
        return hInventoryDao.selectHInventoryByQuery(query);
    }

    /**
     * 保存库存划项目标记
     * @param hInventoryProjectMark
     * @return
     */
    @Transactional
    public boolean saveInvToPro(HInventoryProjectMark hInventoryProjectMark) {
        try {
            //根据主键获取库存信息
            HInventory hInventory = hInventoryDao.selectAllById(hInventoryProjectMark.getIpmBeforeItyId());
            hInventoryProjectMark.setIpmItyType("h");
            hInventoryProjectMark.setIpmProposerId(ShiroUtils.getUser().getId());
            hInventoryProjectMark.setIpmDate(new Date());
            hInventoryProjectMark.setIpmMatDesc(hInventory.getItyMatName());
            hInventoryProjectMark.setIpmMatCode(hInventory.getItyMatCode());
            hInventoryProjectMark.setIpmAppStatus(1);
            //更改库存可用数量
            hInventory.setItyAvaNum(hInventory.getItyAvaNum() - hInventoryProjectMark.getIpmNum());
            if(hInventoryProjectMark.getIpmWeight() != null && hInventoryProjectMark.getIpmWeight() != 0){
                hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() - hInventoryProjectMark.getIpmWeight());
            }
            hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            hInventoryProjectMarkDao.insertSelective(hInventoryProjectMark);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return false;
    }

    /**
     * 加载页面数据
     * @param query
     * @return
     */
    public List<HInventoryProjectMarkModel> selectHInventoryProjectMarkByQuery(HInventoryProjectMarkModel query) {

        return hInventoryProjectMarkDao.selectHInventoryProjectMarkByQuery(query);
    }

    /**
     * 提交标记申请
     * @param hInventoryProjectMarkModel
     * @return
     */
    public boolean submitApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {
        try {
            hInventoryProjectMarkModel.setIpmAppStatus(2);
            hInventoryProjectMarkDao.updateHInvProMarkAppyStatusByIpmIds(hInventoryProjectMarkModel);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 删除标记申请
     * @param hInventoryProjectMarkModel
     * @return
     */
    @Transactional
    public boolean removeApply(HInventoryProjectMarkModel hInventoryProjectMarkModel) {
        try {
            //获取申请列表，往回标记库存
            List<HInventoryProjectMark> hInventoryProjectMarks = hInventoryProjectMarkDao.getHInvProMarkByIpmIds(hInventoryProjectMarkModel.getIpmIds());
            for (int i = 0; i < hInventoryProjectMarks.size(); i++) {
                HInventory hInventory = hInventoryDao.selectAllById(hInventoryProjectMarks.get(i).getIpmBeforeItyId());
                hInventory.setItyAvaNum(hInventory.getItyAvaNum() + hInventoryProjectMarks.get(i).getIpmNum());
                if(hInventory.getItyAvaWeight() != null && hInventoryProjectMarks.get(i).getIpmWeight() != null){
                    hInventory.setItyAvaWeight(hInventory.getItyAvaWeight() + hInventoryProjectMarks.get(i).getIpmWeight());
                }
                hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
            }
            //删除申请列表
            hInventoryProjectMarkDao.deleteHInvProMarkByIpmIds(hInventoryProjectMarkModel.getIpmIds());
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 保存修改
     * @param hInventoryProjectMarkModel
     * @return
     */
    @Transactional
    public boolean update(HInventoryProjectMarkModel hInventoryProjectMarkModel) {
        try {
            hInventoryProjectMarkModel.setIpmAppStatus(1);
            hInventoryProjectMarkDao.updateByPrimaryKeySelective(hInventoryProjectMarkModel);
            HInventory hInventory = new HInventory();
            hInventory.setItyId(hInventoryProjectMarkModel.getIpmBeforeItyId());
            hInventory.setItyAvaNum(hInventoryProjectMarkModel.getItyAvaNum() - hInventoryProjectMarkModel.getIpmNum());
            if(hInventoryProjectMarkModel.getIpmWeight() != null && hInventoryProjectMarkModel.getIpmWeight() != 0){
                hInventory.setItyAvaWeight(hInventoryProjectMarkModel.getItyAvaWeight() - hInventoryProjectMarkModel.getIpmWeight());
            }
            hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * 导出待标记库存在库
     *
     * @param response
     */
    public void inventoryExport(HttpServletResponse response) {

        HInventoryQueryModel queryModel = new HInventoryQueryModel();
        queryModel.setItyStockType(1);
        queryModel.setItyMatTypeQuery(4);
        queryModel.setItyQuery(1);
        List<HInventoryModel> model = hInventoryDao.selectHInventoryModeByQuery(queryModel);
        // 创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        // 建立新的sheet对象（excel的表单）
        HSSFSheet hssfSheet = wb.createSheet("sheet1");
        // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
        HSSFRow hssfRow = hssfSheet.createRow(0);
        // 创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell hssfCell = hssfRow.createCell(0);
        // 合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 14));

        // 设置单元格内容
        hssfCell.setCellValue("导出库存在库" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        hssfRow = hssfSheet.createRow(1);

        hssfRow.createCell(0).setCellValue("库存ID（禁改）");
        hssfRow.createCell(1).setCellValue("SAP物料编码（禁改）");
        hssfRow.createCell(2).setCellValue("物料描述（禁改）");
        hssfRow.createCell(3).setCellValue("规格（禁改）");
        hssfRow.createCell(4).setCellValue("模具号/图号（禁改）");
        hssfRow.createCell(5).setCellValue("项目编号（禁改）");
        hssfRow.createCell(6).setCellValue("项目名称（禁改）");
        hssfRow.createCell(7).setCellValue("可用数量（禁改）");
        hssfRow.createCell(8).setCellValue("库存备注（禁改）");
        hssfRow.createCell(9).setCellValue("挪用项目编号（必填）");
        hssfRow.createCell(10).setCellValue("挪用项目名称（必填）");
        hssfRow.createCell(11).setCellValue("标记数量（必填）");
        hssfRow.createCell(12).setCellValue("划拨说明（必填）");

        // 在sheet里创建第二行
        for (int i = 0; i < model.size(); i++) {
            HSSFRow row = hssfSheet.createRow(i + 2);
            row.createCell(0).setCellValue(model.get(i).getItyId());
            row.createCell(1).setCellValue(model.get(i).getItySapCode());
            row.createCell(2).setCellValue(model.get(i).getItyMatName());
            row.createCell(3).setCellValue(model.get(i).getItySpec());
            row.createCell(4).setCellValue(model.get(i).getItyDrawingNumber());
            row.createCell(5).setCellValue(model.get(i).getItyProCode());
            row.createCell(6).setCellValue(model.get(i).getItyProName());
            row.createCell(7).setCellValue(model.get(i).getItyAvaNum());
            row.createCell(8).setCellValue(model.get(i).getItyRemark());
            row.createCell(9).setCellValue("");
            row.createCell(10).setCellValue("");
            row.createCell(11).setCellValue("");
            row.createCell(12).setCellValue("");
        }
        //输出Excel文件
        try {
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=stockDetails.xls");
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 库存划项目文件导入
     * @param file
     * @return
     */
    @Transactional
    public String importFile(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        HInventoryProjectMark inventoryProjectMark;
        // 错误数据
        List<HImportMarkError> errorList = new ArrayList<>();
        int importCount = 0;    // 导入成功记录数

        Workbook workbook = null;
        try {

            // 判断文件是2007版本还是2010版本
            InputStream is = file.getInputStream();
            if (fileName.endsWith("xlsx")) {
                workbook = new XSSFWorkbook(is);
            }
            if (fileName.endsWith("xls")) {
                workbook = new HSSFWorkbook(is);
            }
            if (workbook != null) {
                //默认读取第一个sheet
                Sheet sheet = workbook.getSheetAt(0);
                HImportItyToProModel importItyToProModel;
                HImportMarkError importMarkError;
                // 排除标题行，从第二行开始导入
                for (int i = sheet.getFirstRowNum() + 2; i <= sheet.getLastRowNum(); i++) {

                    Row row = sheet.getRow(i);
                    // 如果行为空忽略
                    if (row == null) {
                        continue;
                    }
                    // 如果标记内容什么都没填则忽略该行(5 6 7 8)
                    if (StringUtils.isEmpty(getValue(row.getCell(9))) && StringUtils.isEmpty(getValue(row.getCell(10)))
                            && StringUtils.isEmpty(getValue(row.getCell(11))) && StringUtils.isEmpty(getValue(row.getCell(12)))) {
                        continue;
                    }
                    importItyToProModel = new HImportItyToProModel();
                    importMarkError = new HImportMarkError();
                    if(row.getCell(0) != null){
                        importItyToProModel.setItyId(Integer.valueOf(getValue(row.getCell(0))));
                    }
                    importItyToProModel.setItySapCode(getValue(row.getCell(1)));
                    importItyToProModel.setItyMatName(getValue(row.getCell(2)));
                    importItyToProModel.setItySpec(getValue(row.getCell(3)));
                    importItyToProModel.setItyDrawingNumber(getValue(row.getCell(4)));
                    importItyToProModel.setItyProCode(getValue(row.getCell(5)));
                    importItyToProModel.setItyProName(getValue(row.getCell(6)));
                    if(row.getCell(7) != null){
                        importItyToProModel.setItyAvaNum(Double.valueOf(getValue(row.getCell(7))));
                    }
                    importItyToProModel.setItyRemark(getValue(row.getCell(8)));
                    importItyToProModel.setNeedProCode(getValue(row.getCell(9)));
                    importItyToProModel.setNeedProName(getValue(row.getCell(10)));
                    if(row.getCell(11)!=null){
                        importItyToProModel.setNeedNum(Double.valueOf(getValue(row.getCell(11))));
                    }
                    importItyToProModel.setRemark(getValue(row.getCell(112)));
                    // 判断 主要禁改字段是否为空
                    if (importItyToProModel.getItyId() == null) {
                        importMarkError.setImeErrorReason("禁改字段被修改");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    // 取得库存在库
                    HInventory hInventory = hInventoryDao.selectAllById(importItyToProModel.getItyId());
                    // 判断 主要禁改字段是否被修改
                    if (importItyToProModel.getItyId() == null || hInventory == null || !importItyToProModel.getItyId().equals(hInventory.getItyId())) {
                        importMarkError.setImeErrorReason("禁改字段被修改");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    // 判断 导入Excel必填字段是否填写，不填不允许导入
                    if (StringUtils.isEmpty(importItyToProModel.getNeedProCode()) || StringUtils.isEmpty(importItyToProModel.getNeedProName()) || importItyToProModel.getNeedNum() == null) {
                        importMarkError.setImeErrorReason("必填字段为空");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    // 判断 项目编码6位字符，如果是8位或者其他位数 则不允许导入
                    if (importItyToProModel.getNeedProCode()!=null &&importItyToProModel.getNeedProCode().length() != 8) {
                        importMarkError.setImeErrorReason("[项目编号（必填）]字段应该为8位字符串，例如：18K001、18H112等");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    // 判断 项目编码在看板系统是否存在
                    MProCodeQueryModel proCodeQueryModel = new MProCodeQueryModel();
                    proCodeQueryModel.setProCode(importItyToProModel.getNeedProCode());
                    List<ProjectStatusModel> projectStatusModels = orderDetailDao.findProjectByCode(proCodeQueryModel);
                    if (projectStatusModels == null || projectStatusModels.size() == 0) {
                        importMarkError.setImeErrorReason("[项目编号（必填）]输入的项目编号不存在");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    if (importItyToProModel.getNeedNum() > hInventory.getItyAvaNum()) {
                        importMarkError.setImeErrorReason("标记数量大于库存可用数量");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    if (importItyToProModel.getNeedNum() <= 0) {
                        importMarkError.setImeErrorReason("标记数量不能为0或负数");
                        importMarkError = setImportMarkError(importMarkError, importItyToProModel);
                        errorList.add(importMarkError);
                        hImportMarkErrorDao.insertSelective(importMarkError);
                        continue;
                    }
                    // 插入标记表
                    inventoryProjectMark = new HInventoryProjectMark();
                    inventoryProjectMark.setIpmBeforeItyId(hInventory.getItyId());
                    inventoryProjectMark.setIpmNum(importItyToProModel.getNeedNum());
                    if(hInventory.getItyAvaWeight() != null){
                        inventoryProjectMark.setIpmWeight(hInventory.getItyWeight()/hInventory.getItyNum()*importItyToProModel.getNeedNum());
                    }
                    inventoryProjectMark.setIpmDate(new Date());
                    inventoryProjectMark.setIpmProposerId(ShiroUtils.getUser().getId());
                    inventoryProjectMark.setIpmInProjectCode(importItyToProModel.getNeedProCode());
                    inventoryProjectMark.setIpmInProjectName(importItyToProModel.getNeedProName());
                    inventoryProjectMark.setIpmRemark(importItyToProModel.getRemark());
                    inventoryProjectMark.setIpmAppStatus(1); //标记状态：0=删除 1=未提交 2=已提交 3=执行
                    inventoryProjectMark.setIpmItyType("h");
                    inventoryProjectMark.setIpmMatCode(importItyToProModel.getItySapCode());
                    inventoryProjectMark.setIpmMatDesc(importItyToProModel.getItyMatName());
                    hInventoryProjectMarkDao.insertSelective(inventoryProjectMark);

                    // 更新库存表可用数量
                    hInventory.setItyAvaNum(BigDecimalUtils.sub(hInventory.getItyAvaNum(), importItyToProModel.getNeedNum()));
                    if(inventoryProjectMark.getIpmWeight() != null){
                        hInventory.setItyAvaWeight(BigDecimalUtils.sub(hInventory.getItyAvaWeight(), inventoryProjectMark.getIpmWeight()));
                    }
                    hInventoryDao.updateHInventoryAvaNumByPrimary(hInventory);

                    importCount++;
                }

               // logService.saveLog("库存划项目标记导入", "导入成功：" + importCount + "条， " + "导入失败：" + errorList.size() + "条");
                return errorList.size() > 0 ? "导入失败" : "导入完成";
            } else {
                return "导入失败";
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (workbook != null) {
                try {
                    workbook.close();
                } catch (Exception ignored) {
                }
            }
        }
        return "导入失败";
    }

    /**
     * 导入异常数据赋值
     * @param importMarkError
     * @param importItyToProModel
     * @return
     */
    private HImportMarkError setImportMarkError(HImportMarkError importMarkError, HImportItyToProModel importItyToProModel) {
        importMarkError.setImeItyId(importItyToProModel.getItyId());
        importMarkError.setImeUseSapCode(importItyToProModel.getItySapCode());
        importMarkError.setImeUseMatName(importItyToProModel.getItyMatName());
        importMarkError.setImeUseProCode(importItyToProModel.getItyProCode());
        importMarkError.setImeUseProName(importItyToProModel.getItyProName());
        importMarkError.setImeItyAvaNum(importItyToProModel.getItyAvaNum());
        importMarkError.setImeItyRemark(importItyToProModel.getItyRemark());
        importMarkError.setImeNeedProCode(importItyToProModel.getNeedProCode());
        importMarkError.setImeNeedProName(importItyToProModel.getNeedProName());
        importMarkError.setImeMarkNum(importItyToProModel.getNeedNum());
        importMarkError.setImeRemark(importItyToProModel.getRemark());
        importMarkError.setImeMarkUserId(ShiroUtils.getUser().getId());
        importMarkError.setImeMarkDate(new Date());

        return importMarkError;
    }

    /**
     * 导出错误数据
     * @param response
     */
    public void exportErrorExport(HttpServletResponse response) {
        HImportMarkErrorExample ex = new HImportMarkErrorExample();
        ex.setOrderByClause("ime_mark_date desc");
        HImportMarkErrorExample.Criteria criteria = ex.createCriteria();
        criteria.andImeMarkUserIdEqualTo(ShiroUtils.getUserId());  // 登入账号

        // 查询15天之内的异常数据
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(calendar.DAY_OF_MONTH, -15);

        criteria.andImeMarkDateGreaterThanOrEqualTo(calendar.getTime());
        criteria.andImeMarkDateLessThanOrEqualTo(new Date());

        List<HImportMarkError> model = hImportMarkErrorDao.selectByExample(ex);

        // 创建HSSFWorkbook对象(excel的文档对象)
        HSSFWorkbook wb = new HSSFWorkbook();
        // 建立新的sheet对象（excel的表单）
        HSSFSheet hssfSheet = wb.createSheet("sheet1");
        // 在sheet里创建第一行，参数为行索引(excel的行)，可以是0～65535之间的任何一个
        HSSFRow hssfRow = hssfSheet.createRow(0);
        // 创建单元格（excel的单元格，参数为列索引，可以是0～255之间的任何一个
        HSSFCell hssfCell = hssfRow.createCell(0);
        // 合并单元格CellRangeAddress构造参数依次表示起始行，截至行，起始列， 截至列
        hssfSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 14));

        // 设置单元格内容
        hssfCell.setCellValue("导出标记异常数据" + DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        hssfRow = hssfSheet.createRow(1);

        hssfRow.createCell(0).setCellValue("库存ID（禁改）");
        hssfRow.createCell(1).setCellValue("SAP物料编码（禁改）");
        hssfRow.createCell(2).setCellValue("物料描述（禁改）");
        hssfRow.createCell(3).setCellValue("项目编号（禁改）");
        hssfRow.createCell(4).setCellValue("项目名称（禁改）");
        hssfRow.createCell(5).setCellValue("可用数量（禁改）");
        hssfRow.createCell(6).setCellValue("库存备注（禁改）");
        hssfRow.createCell(7).setCellValue("挪用项目编号（必填）");
        hssfRow.createCell(8).setCellValue("挪用项目名称（必填）");
        hssfRow.createCell(9).setCellValue("标记数量（必填）");
        hssfRow.createCell(10).setCellValue("划拨说明（必填）");
        hssfRow.createCell(11).setCellValue("异常原因");
        hssfRow.createCell(12).setCellValue("导入日期");

        // 在sheet里创建第二行
        for (int i = 0; i < model.size(); i++) {
            HSSFRow row = hssfSheet.createRow(i + 2);
            if(model.get(i).getImeItyId()!=null){
                row.createCell(0).setCellValue(model.get(i).getImeItyId());
            }
            if(model.get(i).getImeUseSapCode()!=null){
                row.createCell(1).setCellValue(model.get(i).getImeUseSapCode());
            }
            if(model.get(i).getImeUseMatName()!=null){
                row.createCell(2).setCellValue(model.get(i).getImeUseMatName());
            }
            if(model.get(i).getImeUseProCode()!=null){
                row.createCell(3).setCellValue(model.get(i).getImeUseProCode());
            }
            if(model.get(i).getImeUseProName()!=null){
                row.createCell(4).setCellValue(model.get(i).getImeUseProName());
            }
            if(model.get(i).getImeItyAvaNum()!=null){
                row.createCell(5).setCellValue(model.get(i).getImeItyAvaNum());
            }
            if(model.get(i).getImeItyRemark()!=null){
                row.createCell(6).setCellValue(model.get(i).getImeItyRemark());
            }
            if(model.get(i).getImeNeedProCode()!=null){
                row.createCell(7).setCellValue(model.get(i).getImeNeedProCode());
            }
            if(model.get(i).getImeNeedProName()!=null){
                row.createCell(8).setCellValue(model.get(i).getImeNeedProName());
            }
            if(model.get(i).getImeMarkNum()!=null){
                row.createCell(9).setCellValue(model.get(i).getImeMarkNum());
            }
            if(model.get(i).getImeRemark()!=null){
                row.createCell(10).setCellValue(model.get(i).getImeRemark());
            }
            row.createCell(11).setCellValue(model.get(i).getImeErrorReason());
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            row.createCell(12).setCellValue(dateFormat.format(model.get(i).getImeMarkDate()));

        }
        //输出Excel文件
        try {
            OutputStream output = response.getOutputStream();
            response.reset();
            response.setHeader("Content-disposition", "attachment; filename=importError.xls");
            response.setContentType("application/msexcel");
            wb.write(output);
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除异常数据表数据
     * @param UserId
     * @return
     */
    public int deleteFileByUserId(Integer UserId) {

        return hImportMarkErrorDao.deleteHImportMarkErrorByUserId(UserId);
    }
}
